package com.cgah

import javax.xml.bind.annotation.XmlAccessType
import javax.xml.bind.annotation.XmlAccessorType

@XmlAccessorType(XmlAccessType.FIELD)
class User {

    String emailAddress
	String username
	String password
    // true by default dammit
	boolean enabled = true;
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired

   static transients = ['shortName', 'displayName'];

	static constraints = {
		username(blank: false, unique: true)
		// 6 to eight character constant is stupid!!!! stupid!!! I would never do this
		// it is insecure and god just really stupid in all sense of stupidity, but I will
		// put it here because it's in the assignment.
        password(password: true, size:6..8, blank: false, nullable:false);
        emailAddress(email:true, blank:false, nullable:false, unique:true);
	}

	static mapping = {
		password column: '`password`'
	}

	Set<Role> getAuthorities() {
		UserRole.findAllByUser(this).collect { it.role } as Set
	}

    String getShortName() {
        return emailAddress?.substring(0, emailAddress?.indexOf("@"));
    }

    String getDisplayName() {
        return (username ? username : getShortName());
    }

}
